$(function() {

	// init date tables
	var logTable = $("#api_list").dataTable({
		"deferRender": true,
		"processing" : true, 
	    "serverSide": true,
		"ajax": {
	        url: "list" ,
	        data : function ( d ) {
	        	var obj = {};
                obj.start = d.start;
                obj.length = d.length;
                return obj;
            }
	    },
	    "searching": false,
	    "ordering": false,
	    //"scrollX": false,
	    "columns": [
	                {
						"data": 'id',
						"render": function ( data, type, row ) {
							return  row.id;
						}
					},
					{
						"data": 'type',
						"render": function ( data, type, row ) {
                            return row.type;
						}
	                },
	                {
						"data": 'name',
						"render": function ( data, type, row ) {
                            return row.name;
						}
	                },
	                { 
	                	"data": 'key',
	                	"render": function ( data, type, row ) {
                            return row.key;
                        }
	                },
                    { 
	                	"data": 'api',
	                	"render": function ( data, type, row ) {
                            return row.api;
                        }
	                },
                    { 
	                	"data": 'key',
	                	"render": function ( data, type, row ) {
                            return row.status;
                        }
	                },
                    { 
	                	"data": 'status',
	                	"render": function ( data, type, row ) {
                            return "<button  data-id=\""+ row.id +"\" class=\"btn btn-primary btn-xs job_operate api_operate_edit\">修改</button>&nbsp;<button  data-id=\""+ row.id +"\" class=\"btn btn-primary btn-xs job_operate api_operate_delete\">删除</button>";
                        }
	                }
                    
	            ],
		"language" : {
			"sProcessing" : "处理中...",
			//"sLengthMenu" : "每页 _MENU_ 条记录",
			"sLengthMenu" : "",
			"sZeroRecords" : "没有匹配结果",
			"sInfo" : "第 _PAGE_ 页 ( 总共 _PAGES_ 页，_TOTAL_ 条记录 )",
			"sInfoEmpty" : "无记录",
			"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
			"sInfoFiltered" : false,
			"sInfoPostFix" : "",
			"sSearch" : "搜索:",
			"sUrl" : "",
			"sEmptyTable" : "表中数据为空",
			"sLoadingRecords" : "载入中...",
			"sInfoThousands" : ",",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上页",
				"sNext" : "下页",
				"sLast" : "末页"
			},
			"oAria" : {
				"sSortAscending" : ": 以升序排列此列",
				"sSortDescending" : ": 以降序排列此列"
			}
		}
	});
	
    //提交表单
	$("#addAPI").submit(function(){
        $.post('add', $(this).serialize(), function(data){
            if(data.code=200)
            {
                layer.open({
                    title: '系统提示',
                    content:  "新增api成功",
                    icon: '1',
                    end: function(layero, index){
                            $('#addModal').modal('hide');
                            logTable.fnDraw();
                    }
                });
            } else {
                layer.open({
                    title: '系统提示',
                    content:  "新增api失败:" ,
                    icon: '1',
                    end: function(layero, index){
 
                    }
                });
                return false;
            }
        }); 
        return false;
    });
    
    // 新增api
	$("#addapi").click(function(){
		$('#addModal').modal({backdrop: false, keyboard: false}).modal('show');
	});
    
    $("#api_list").delegate(".api_operate_edit", "click", function(){
        console.log("aaaaa");
        
    });
    
    $("#api_list").delegate(".api_operate_delete","click", function(){
        $.post("delete", {id: $(this).data("id")}, function(data){
            if(data.code==200)
            {
                layer.open({
                    title: '系统提示',
                    content:  "删除api成功",
                    icon: '1',
                    end: function(layero, index){
                            logTable.fnDraw();
                    }
                });
            } else {
                layer.open({
                    title: '系统提示',
                    content:  "删除api失败！" ,
                    icon: '1',
                    end: function(layero, index){
                    
                    }
                });
                return false;
            }   
        });
    });

});


// 提示-科技主题
var ComAlertTec = {
	html:function(){
		var html =
			'<div class="modal fade" id="ComAlertTec" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
			'<div class="modal-dialog">' +
			'<div class="modal-content-tec">' +
			'<div class="modal-body"><div class="alert" style="color:#fff;"></div></div>' +
			'<div class="modal-footer">' +
			'<div class="text-center" >' +
			'<button type="button" class="btn btn-info ok" data-dismiss="modal" >确认</button>' +
			'</div>' +
			'</div>' +
			'</div>' +
			'</div>' +
			'</div>';
		return html;
	},
	show:function(msg, callback){
		// dom init
		if ($('#ComAlertTec').length == 0){
			$('body').append(ComAlertTec.html());
		}

		// 弹框初始
		$('#ComAlertTec .alert').html(msg);
		$('#ComAlertTec').modal('show');

		$('#ComAlertTec .ok').click(function(){
			$('#ComAlertTec').modal('hide');
			if(typeof callback == 'function') {
				callback();
			}
		});
	} 
};









